﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CodeRun.Utils.CommandLine
{
    internal class CommandLineArgumentsTokenizer
    {
        private readonly StringBuilder Processed = new StringBuilder();
        private StringBuilder CurrentToken;
        private bool InString;
        private List<string> Tokens;

        public string[] Tokenize(string text)
        {
            Tokens = new List<string>();
            CurrentToken = new StringBuilder();
            InString = false;
            foreach (char ch in text)
            {
                if (ch == '\"')
                {
                    if (InString)
                    {
                        InString = false;
                        ContinueToken(ch);
                    }
                    else
                    {
                        InString = true;
                        BeginOrContinueToken(ch);
                    }
                }
                else if (ch == ' ')
                {
                    if (InString)
                        ContinueToken(ch);
                    else
                        EndTokenIfExists();
                }
                else
                {
                    BeginOrContinueToken(ch);
                }
                Processed.Append(ch);
            }
            EndTokenIfExists();
            return Tokens.ToArray();
        }


        private void BeginToken(char ch)
        {
            BeginToken();
            CurrentToken.Append(ch);
        }

        private void BeginToken()
        {
            if (CurrentToken != null)
                throw new Exception("cannot begin word in the middle of another word");
            CurrentToken = new StringBuilder();
        }

        private void BeginOrContinueToken(char ch)
        {
            if (CurrentToken == null)
                CurrentToken = new StringBuilder();
            CurrentToken.Append(ch);
        }

        private void ContinueToken(char ch)
        {
            CurrentToken.Append(ch);
        }

        private void EndToken()
        {
            string s = CurrentToken.ToString();
            if (s.IsNotNullOrEmpty())
                Tokens.Add(s);
            CurrentToken = null;
        }

        private void EndTokenIfExists()
        {
            if (CurrentToken != null)
                Tokens.Add(CurrentToken.ToString());
            CurrentToken = null;
        }
    }
}